package com.kg.project02.server.server.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kg.project02.server.server.Model.User;
import com.kg.project02.server.server.Service.UserService;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Resource
    UserService userService;

    @GetMapping("/show/all")
    public List<User> show_user_all(){

        return userService.show_user(null);
    }

    @GetMapping("/show/page")
    public IPage<User> show_user_byPage(@RequestParam("page") int page, @RequestParam("size") int size){

        IPage<User> userIPage = new Page<>(page,size);
        return userService.show_user_byPage(userIPage,null);
    }

    @PostMapping("/create")
    public int create_user(@RequestBody User user){

        return userService.create_user(user);
    }

    @DeleteMapping("/delete")
    public int delete_user(@RequestParam("username") String username,
                           @RequestParam("password") String password){

        User user =new User();
        user.setUserName(username);
        user.setPassword(password);
        QueryWrapper<User> wrapper = new QueryWrapper<>(user);
        return userService.delete_user(wrapper);
    }

    @PutMapping("/update")
    public int update_user(@RequestBody User user){

        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_name",user.getUserName()).eq("password",user.getPassword());
        return userService.update_user(user,wrapper);
    }

    @GetMapping("/search")
    public IPage<User> search_user(@RequestParam(value = "page",required = false,defaultValue = "1") int page,
                                   @RequestParam(value = "size",required = false,defaultValue = "5") int size,
                                   @RequestParam(value = "username",required = false,defaultValue = "") String username,
                                   @RequestParam(value = "name",required = false,defaultValue = "") String name,
                                   @RequestParam(value = "age",required = false,defaultValue = "") String age,
                                   @RequestParam(value = "email",required = false,defaultValue = "") String email){

        IPage<User> userIPage = new Page<>(page,size);
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        if(!username.isEmpty()){
            wrapper.eq("user_name",username);
        }
        if(!name.isEmpty()){
            wrapper.eq("name",name);
        }
        if(!age.isEmpty()){
            wrapper.eq("age",age);
        }
        if(!email.isEmpty()){
            wrapper.eq("email",email);
        }
        return userService.show_user_byPage(userIPage,wrapper);
    }
}
